using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using LiveStock.dataAccess;

namespace LiveStock
{
    public partial class Weight : FadeForm
    {
        
        private CurrentUser _curUser;
        private int whichSubmit; //a variable to keep track of which button was pressed to create the object. 0 = add 1 = modify
        private Word passedWord;
        public Weight(CurrentUser curUser)
        {
            InitializeComponent();
            Weight_txtWeight.Text = "0";
            whichSubmit = 0;
            _curUser = curUser;            
        }

        public Weight(Word inWord, CurrentUser curUser)
        {
            InitializeComponent();
            Weight_txtField.Text = inWord.KeyWord;
            Weight_txtWeight.Text = inWord.Weight.ToString();
            Weight_tkbWeightSlider.Value = inWord.Weight;
            whichSubmit = 1;
            passedWord = inWord;
            _curUser = curUser;
        }

        private void Weight_tkbWeightSlider_Scroll(object sender, EventArgs e)
        {
            Weight_txtWeight.Text = Weight_tkbWeightSlider.Value.ToString();
        }

        private void Weight_btnSave_Click(object sender, EventArgs e)
        {
            if (!Weight_txtField.Text.Equals(""))
            {
                Word newRow = new Word();
                if (whichSubmit == 0)
                {
                    //Add the word to the database, make sure to test that it doesn't exist first.  
                    if (!KeywordsData.WordAlreadySaved(Weight_txtField.Text, _curUser.UserID))
                    {
                        newRow.KeyWord = Weight_txtField.Text;
                        newRow.Weight = Weight_tkbWeightSlider.Value;
                        newRow.UserID = _curUser.UserID;
                        newRow.DefaultWeight = 0;
                        KeywordsData.KeywordInsert(newRow);
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("The word you have entered already exists.");
                    }
                }
                else if (whichSubmit == 1)
                {
                    //Updated the database with the changed information.
                    newRow.KeyWordID = passedWord.KeyWordID;
                    newRow.KeyWord = Weight_txtField.Text;
                    newRow.Weight = Weight_tkbWeightSlider.Value;
                    newRow.UserID = _curUser.UserID;
                    KeywordsData.KeywordUpdate(newRow);
                    this.Close();
                }
                else
                {
                    MessageBox.Show("An unexpected error was encountered.");
                }
            }
            else
            {
                MessageBox.Show("You must enter a value.");
            }
        }


    }
}